package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;
    @Reference
    private RoleService roleService;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername() {
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if (user != null) {
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //新增用户管理
    @RequestMapping("/add")
    public Result add(Integer[] roles, @RequestBody User user) {
        try {
            userService.add(roles, user);
            return new Result(true, MessageConstant.ADD_USERMANAGER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USERMANAGER_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return userService.findPage(queryPageBean);
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] roles, @RequestBody User user) {
        Integer userId = user.getId();
        System.out.println(userId);
        //修改用户管理
        try {
            userService.edit(roles, user);
            return new Result(true, MessageConstant.EDIT_USERMANAGER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USERMANAGER_FAIL);
        }
    }

    //业务的回显功能,前端接受的是一个ajax发送的请求,需要多个参数,后端用map进行封装
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        //根据这个查询到用户的基本信息  查到中间表关联的角色的集合  根据中间表查询对应的角色基本信息
        try {
            Map<String, Object> map = userService.fingById(id);
            return new Result(true, MessageConstant.GET_USERMANAGER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERMANAGER_FAIL);
        }

    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            userService.delete(id);
            return new Result(true, MessageConstant.DELETE_USERMANAGER_SUCCESS);
        } catch (RuntimeException r) {
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USERMANAGER_FAIL);
        }
    }
}
